#!/usr/bin/env python
# -*- coding:utf-8 -*-

# src/grujieclient/ruijieconf.py

# Copyright (C) 2009 Gong Han <gong AT fedoraproject.org>

# This file is part of gRuijieClient.

# gRuijieClient is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.

# gRuijieClient is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with gRuijieClient. If not, see <http://www.gnu.org/licenses/>.

import subprocess
import sys
from string import atoi, join
from xml.dom import minidom
from globe import *

class RuijieConf:
    """
    RuijieClient configuration operating class
    @author: Gong Han
    """
    def __init__(self, path):

        self._path = path
        try:
            self._rjdom = minidom.parse(path)
        except:
            try:
                # call 'ruijieclient -g' to generate a sample conf
                if subprocess.call(['ruijieclient', '-g']) == 0:
                    self._rjdom = minidom.parse(path)
            except:
                print >> sys.stderr, 'fail in executing ruijieclient'
                sys.exit(1)

    def load(self):
        # load values from file
        self.Name = self.get_value('Name')
        self.Password = self.get_value('Password')
        self.AuthenticationMode = self.get_value('AuthenticationMode')
        self.NIC = self.get_value('NIC')
        self.EchoInterval = self.get_value('EchoInterval')
        self.IntelligentReconnect = self.get_value('IntelligentReconnect')
        self.FakeVersion = self.get_value('FakeVersion')

        try:
            self._ver1, self._ver2 = self.FakeVersion.split('.')
        except:
            self._ver1 = self._ver2 = ''

        self.FakeAddress = self.get_value('FakeAddress')
        self.HCmode = self.get_value('DHCPmode')
        self.AutoConnect = self.get_value('AutoConnect')
        # load widgets from glade
        self._entry_name = GladeUI.get_widget('entry_name')
        self._entry_pwd = GladeUI.get_widget('entry_pwd')
        self._combobox_authmode = GladeUI.get_widget('combobox_authmode')
        self._combobox_HCmode = GladeUI.get_widget('combobox_HCmode')
        self._entry_echo = GladeUI.get_widget('entry_echo')
        self._entry_ip = GladeUI.get_widget('entry_ip')
        self._entry_ver1 = GladeUI.get_widget('entry_ver1')
        self._entry_ver2 = GladeUI.get_widget('entry_ver2')
        self._checkbutton_ip = GladeUI.get_widget('checkbutton_ip')
        self._checkbutton_ver = GladeUI.get_widget('checkbutton_ver')
        self._checkbutton_autocon = GladeUI.get_widget('checkbutton_autocon')
        self._checkbutton_recon = GladeUI.get_widget('checkbutton_recon')
        # show values in gtk entry
        self._entry_name.set_text(self.Name)
        self._entry_pwd.set_text(self.Password)
        self._combobox_authmode.set_active(atoi(self.AuthenticationMode))
        self._combobox_HCmode.set_active(atoi(self.HCmode))
        self._entry_echo.set_text(self.EchoInterval)

        if self.FakeAddress != '':
            self._checkbutton_ip.set_active(True)

        self._entry_ip.set_text(self.FakeAddress)

        if self.FakeVersion != '':
            self._checkbutton_ver.set_active(True)

        self._entry_ver1.set_text(self._ver1)
        self._entry_ver2.set_text(self._ver2)

        if self.IntelligentReconnect == '1':
            self._checkbutton_recon.set_active(True)
        if self.AutoConnect == '1':
            self._checkbutton_autocon.set_active(True)

    def save(self):
        # save settings
        self.Name = self._entry_name.get_text()
        self.Password = self._entry_pwd.get_text()
        self.AuthenticationMode = str(self._combobox_authmode.get_active())
        self.HCmode = str(self._combobox_HCmode.get_active())
        self.EchoInterval = self._entry_echo.get_text()

        if self._checkbutton_ip.get_active():
            self.FakeAddress = self._entry_ip.get_text()
        else:
            self.FakeAddress = ''

        if self._checkbutton_ver.get_active():
            self._ver1 = self._entry_ver1.get_text()
            self._ver2 = self._entry_ver2.get_text()
            self.FakeVersion = join([self._ver1, self._ver2], ('.'))
        else:
            self.FakeVersion = ''

        if self._checkbutton_recon.get_active():
            self.IntelligentReconnect = '1'
        else:
            self.IntelligentReconnect = '0'

        if self._checkbutton_autocon.get_active():
            self.AutoConnect = '1'
        else:
            self.AutoConnect = '0'

        self.set_value('Name', self.Name)
        self.set_value('Password', self.Password)
        self.set_value('AuthenticationMode', self.AuthenticationMode)
        self.set_value('NIC', self.NIC)
        self.set_value('EchoInterval', self.EchoInterval)
        self.set_value('IntelligentReconnect', self.IntelligentReconnect)
        self.set_value('FakeVersion',self.FakeVersion)
        self.set_value('FakeAddress', self.FakeAddress)
        self.set_value('DHCPmode', self.HCmode)
        self.set_value('AutoConnect', self.AutoConnect)
        # save to file
        try:
            file = open(self._path, 'w')
            self._rjdom.writexml(file)
        except:
            print >> sys.stderr, 'fail in saving configuration'   

    def get_value(self, tag):
        try:
            # seach for tag
            tmpnode = self._rjdom.getElementsByTagName(tag)
            # get element's content
            tmpvalue = tmpnode[0].childNodes[0].nodeValue
            return tmpvalue
        except:
            return ''

    def set_value(self, tag, value):
        tmpnode = self._rjdom.getElementsByTagName(tag)
        if len(tmpnode[0].childNodes) != 0:
            tmpnode[0].childNodes[0].nodeValue = value
        else:
            indentNode = self._rjdom.createTextNode(value)
            tmpnode[0].appendChild(indentNode)